package com.metaera.app.data.rx;


import com.metaera.app.controller.core.RequestPresenter;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.model.BasicListResponse;

public class PaginationResponseObserver<T> extends ResponseObserver<BasicListResponse<T>> {

    private final int mPage;

    private final IPaginationView<T> mView;

    public PaginationResponseObserver(RequestPresenter presenter, IPaginationView<T> view, int page) {
        super(presenter, view, null);
        this.mPage = page;
        this.mView = view;
    }


    @Override
    public void onNext(BasicListResponse<T> entity) {
        if (!entity.isSucceed()){
            super.onNext(entity);
            return;
        }

        BasicListResponse.PageDataEntity<T> pageDataEntity = entity.getData();

        if (mPage <= 1) {
            mView.renderRefresh(pageDataEntity.getList());

        } else {
            mView.renderLoadMore(pageDataEntity.getList());
        }

        if (!pageDataEntity.hasMore()) {
            mView.renderNoMore();
        }

        mView.complete();
    }

}
